package org.example.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Update;
import org.example.bean.User;
import org.example.service.UserService;
import org.example.util.JsonResult;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther:Liu
 * @Testname:UserController
 * @Date:2025/8/5 11:33
 */
@RestController
@RequestMapping("/User")
public class UserController {
    @Resource
    private UserService userService;

    //    查找数据
    @GetMapping("/{page}/{size}")
    public JsonResult findAll(@PathVariable("page") int page, @PathVariable("size") int size) {
        return userService.find(page, size);
    }

    //添加数据
    @PostMapping("/saveUser")
    public JsonResult UserSave(User user) {
        return userService.saveUser(user);
    }

    //查找姓名是否重复
    @GetMapping("/findUserNameCount")
    public JsonResult findUserNameCount(String username) {
        return userService.findUserNameCount(username);
    }

    //查找电话是否重复
    @GetMapping("/findUserPhoneCount")
    public JsonResult findUserPhoneCount(String phone) {
        return userService.findUserPhoneCount(phone);
    }

    //查找邮箱是否重复
    @GetMapping("/findUserEmailCount")
    public JsonResult findUserEmailCount(String email) {
        return userService.findUserEmailCount(email);
    }

    //    修改状态
    @PutMapping("/status/{id}/{status}")
    public JsonResult changeStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return userService.changeStatus(id, status);
    }

    //    查找一条数据
    @GetMapping("/findById/{id}")
    public JsonResult findUserById(@PathVariable("id") String id) {
        return userService.findUserById(id);
    }

    //    更新用户信息
    @PutMapping("/update")
    public JsonResult updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    //    删除数据
    @DeleteMapping("/{id}")
    public JsonResult deleteUser(@PathVariable("id") String id) {
        return userService.deleteUser(id);
    }

    //登录
    @PostMapping("/login")
    public JsonResult login(String username, String password) {
        return userService.findUserInUsers(username, password);
    }

    //    验证登录
    @GetMapping("/isLogin/{id}")
    public JsonResult isLogin(@PathVariable("id") String id) {
        return userService.isLogin(id);
    }


    //生成令牌
    @PostMapping("/getToken/{username}")
    public JsonResult findUser(@PathVariable("username")String username) {
        return userService.getJwtToken(username);
    }

//判断令牌是否存在
    @GetMapping("/is_login")
    public JsonResult IsLogin(HttpServletRequest request) {
        return userService.IsLogin(request.getHeader("token"));
    }

    //令牌退出
    @DeleteMapping("/loginout")
    public JsonResult Loginout(HttpServletRequest request) {
        return userService.Loginout(request.getHeader("token"));
    }
}
